<template>
  <c-form-dialog
    ref="form"
    :visible.sync="innerVisible"
    :form-items="formItems"
    :form-data="formData"
    :table-name="tableName"
    :table-id="tableId"
    @close="handleClose"
    @success="handleSuccess"
    :before-save="handleBeforeSave"
  ></c-form-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import { enumRenewType } from '@/credit/views/city/bus/normal/postloan/enum'
export default {
  components: {},
  props: {
    visible: Boolean,
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      innerVisible: false,
      tableName: 'renewal_loan_28',
      formItems: [
        {
          tag: 'select',
          itemAttrs: {
            label: '续贷类型',
            rules: [{ required: true, message: '请选择续贷类型', trigger: 'change' }],
          },
          attrs: {
            key: 'renew_type',
            value: '',
            options: enumRenewType,
          },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '续贷金额',
            rules: [{ required: true, message: '请输入续贷金额', trigger: 'blur' }],
          },
          attrs: {
            key: 'renew_funds',
            placeholder: '请输入续贷金额',
          },
          tagSlots: [{ name: 'append', slot: '万元' }],
          directives: [
            {
              name: 'input-number',
              value: {
                min: 0,
                fixed: 4,
              },
            },
          ],
          // events: {
          //   input: (model, v) => {
          //     const funds =
          //       this.formData.left_funds ||
          //       this.formData.cur_left_funds ||
          //       this.formData.warranty_amount
          //     if (funds && v > +(funds || '')) {
          //       model.renew_funds = funds
          //     }
          //   },
          // },
        },
        {
          tag: 'date',
          itemAttrs: {
            label: '续贷开始时间',
            rules: [{ required: true, message: '请选择续贷到期时间', trigger: 'change' }],
          },
          attrs: { key: 'renew_loan_start_date', value: '', placeholder: '请选择续贷到期时间' },
        },
        {
          tag: 'date',
          itemAttrs: {
            label: '续贷结束时间',
            rules: [{ required: true, message: '请选择续贷到期时间', trigger: 'change' }],
          },
          attrs: { key: 'renew_loan_end_date', value: '', placeholder: '请选择续贷到期时间' },
        },
        {
          tag: 'upload',
          itemAttrs: {
            label: '续贷附件',
          },
          attrs: {
            class: 'form-control-ml',
            key: 'exh_attachment',
            value: '',
            placeholder: '续贷附件',
            accept: '',
            fileName: 0,
            listType: '',
            dir: `/bank/limit/period/${+new Date()}/${this.tableId}`,
          },
        },
      ],
    }
  },
  computed: {
    ...mapGetters(['user', 'clientId']),
    tableId() {
      // 从其他表查的数据确保没有id这个字段
      return this.formData.id || ''
    },
  },
  watch: {
    visible(val) {
      this.innerVisible = val
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleBeforeSave(model) {
      model.customer_related_id = this.formData.customer_related_id
      return model
    },
    handleClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess(res) {
      this.$emit('success', res)
    },
  },
}
</script>
<style lang="scss" scoped></style>
